/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package boundary_package;

import Entitys.TipoProducto;
import Helpers.RegexFormatter;
import Helpers.RegularExpression;
import control_package.AutoCompleteControl;
import control_package.BussinesControl;
import java.awt.BorderLayout;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import services.TipoProductoServices;
import javax.swing.table.DefaultTableModel;
import javax.swing.text.BadLocationException;

public class ModifyDataGui extends javax.swing.JPanel {
     private String[] labels={"IdProducto", "Nombre de Producto","Precio", "Tipo"};
     private TipoProductoServices tBS=new TipoProductoServices();
     private DefaultComboBoxModel TipoComboboxModel=new DefaultComboBoxModel();
     private DefaultTableModel tableModel =new DefaultTableModel(labels, 1);
     private BussinesControl buscon=new BussinesControl();
     private AutoCompleteControl aCControl= new AutoCompleteControl();
   

/**
 *
 * @author Administrador
 */


    /**
     * Creates new form ModifyDataGui
     */
    public ModifyDataGui() {
        initComponents();
        TipoComboBoxModelFill();
        this.EditjTable.setModel(tableModel);
        integrity();
    }

    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        NamejFormattedTextField = new javax.swing.JFormattedTextField();
        IdjFormattedTextField = new javax.swing.JFormattedTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        OkjButton = new javax.swing.JButton();
        NewPricejFormattedTextField = new javax.swing.JFormattedTextField();
        NewNamejFormattedTextField = new javax.swing.JFormattedTextField();
        DeletejButton = new javax.swing.JButton();
        SearchjButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        EditjTable = new javax.swing.JTable();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        AdvanceEdicionjButton = new javax.swing.JButton();
        TipojComboBox = new javax.swing.JComboBox();

        setBorder(javax.swing.BorderFactory.createTitledBorder(null, "MODIFICAR DATOS", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Verdana", 0, 18))); // NOI18N
        setPreferredSize(new java.awt.Dimension(1240, 640));

        NamejFormattedTextField.setFont(new java.awt.Font("Verdana", 0, 24)); // NOI18N
        NamejFormattedTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                NamejFormattedTextFieldActionPerformed(evt);
            }
        });
        NamejFormattedTextField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                NamejFormattedTextFieldFocusLost(evt);
            }
        });
        NamejFormattedTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                NamejFormattedTextFieldKeyReleased(evt);
            }
        });

        IdjFormattedTextField.setFont(new java.awt.Font("Verdana", 0, 24)); // NOI18N
        IdjFormattedTextField.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseMoved(java.awt.event.MouseEvent evt) {
                IdjFormattedTextFieldMouseMoved(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Verdana", 0, 24)); // NOI18N
        jLabel1.setText("Nombre de Producto:");

        jLabel2.setFont(new java.awt.Font("Verdana", 0, 24)); // NOI18N
        jLabel2.setText("Id de Producto:");

        OkjButton.setFont(new java.awt.Font("Verdana", 0, 18)); // NOI18N
        OkjButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icons/Copia de icon_check.png"))); // NOI18N
        OkjButton.setText("ACEPTAR");

        NewPricejFormattedTextField.setFont(new java.awt.Font("Verdana", 0, 24)); // NOI18N

        NewNamejFormattedTextField.setFont(new java.awt.Font("Verdana", 0, 24)); // NOI18N
        NewNamejFormattedTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                NewNamejFormattedTextFieldActionPerformed(evt);
            }
        });

        DeletejButton.setFont(new java.awt.Font("Verdana", 0, 18)); // NOI18N
        DeletejButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icons/cross.png"))); // NOI18N
        DeletejButton.setText("BORRAR");

        SearchjButton.setFont(new java.awt.Font("Verdana", 0, 18)); // NOI18N
        SearchjButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icons/kappfinder.png"))); // NOI18N
        SearchjButton.setText("BUSCAR");

        EditjTable.setFont(new java.awt.Font("Verdana", 0, 18)); // NOI18N
        EditjTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        EditjTable.setRowHeight(18);
        jScrollPane1.setViewportView(EditjTable);

        jLabel3.setFont(new java.awt.Font("Verdana", 0, 24)); // NOI18N
        jLabel3.setText("Nuevo precio:");

        jLabel4.setFont(new java.awt.Font("Verdana", 0, 24)); // NOI18N
        jLabel4.setText("nuevo nombre:");

        jLabel5.setFont(new java.awt.Font("Verdana", 0, 24)); // NOI18N
        jLabel5.setText("Nuevo tipo:");

        AdvanceEdicionjButton.setFont(new java.awt.Font("Verdana", 0, 24)); // NOI18N
        AdvanceEdicionjButton.setText("AGERGAR PRODUCTOS");
        AdvanceEdicionjButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AdvanceEdicionjButtonActionPerformed(evt);
            }
        });

        TipojComboBox.setFont(new java.awt.Font("Verdana", 0, 24)); // NOI18N
        TipojComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 569, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(192, 192, 192)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4)
                            .addComponent(NewPricejFormattedTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel5)
                            .addComponent(NewNamejFormattedTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 279, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(SearchjButton, javax.swing.GroupLayout.PREFERRED_SIZE, 210, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(DeletejButton, javax.swing.GroupLayout.PREFERRED_SIZE, 184, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(43, 43, 43)
                                .addComponent(OkjButton, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(TipojComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 194, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(IdjFormattedTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 187, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(50, 50, 50)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(NamejFormattedTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 734, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 272, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addComponent(AdvanceEdicionjButton)))
                .addContainerGap(237, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(IdjFormattedTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(NamejFormattedTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(11, 11, 11)
                .addComponent(SearchjButton, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(NewPricejFormattedTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel4)
                        .addGap(4, 4, 4)
                        .addComponent(NewNamejFormattedTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(TipojComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(49, 49, 49)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(AdvanceEdicionjButton)
                    .addComponent(DeletejButton, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(OkjButton, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(87, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents
public void TipoComboBoxModelFill(){
     
            this.TipoComboboxModel.addElement("Seleccione");
           for(TipoProducto tip:tBS.readAll()){
           this.TipoComboboxModel.addElement(tip.getTipo());
           }
       TipojComboBox.setModel(TipoComboboxModel);
 }
    
    private void NamejFormattedTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_NamejFormattedTextFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_NamejFormattedTextFieldActionPerformed

    private void NewNamejFormattedTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_NewNamejFormattedTextFieldActionPerformed
        
    }//GEN-LAST:event_NewNamejFormattedTextFieldActionPerformed

    private void AdvanceEdicionjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AdvanceEdicionjButtonActionPerformed
        this.setVisible(false);
        this.removeAll();
        this.setLayout(new BorderLayout());
        this.add(new EditionAdvance(), BorderLayout.CENTER);
        this.setVisible(true);
        
        
        
    }//GEN-LAST:event_AdvanceEdicionjButtonActionPerformed

    private void IdjFormattedTextFieldMouseMoved(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_IdjFormattedTextFieldMouseMoved
        if (!this.IdjFormattedTextField.getText().isEmpty()){
            String s=buscon.SearchNameById(Long.parseLong(this.IdjFormattedTextField.getText()));
            
            if(NamejFormattedTextField.getText().isEmpty()){
                this.NamejFormattedTextField.setText(s);
            }else if(!NamejFormattedTextField.getText().equals(s)){
                this.NamejFormattedTextField.setText(s);    
            }
        }
    }//GEN-LAST:event_IdjFormattedTextFieldMouseMoved

    private void NamejFormattedTextFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_NamejFormattedTextFieldKeyReleased
        if(evt.getKeyCode()!=8 && evt.getKeyCode()!= 127){
             if(!NamejFormattedTextField.getText().isEmpty())
                try {
                    aCControl.AddTextToTextField(NamejFormattedTextField,NamejFormattedTextField.getText(),true);
                } catch (BadLocationException ex) {
                    Logger.getLogger(SellGui.class.getName()).log(Level.SEVERE, null, ex);
             }
        }  
    }//GEN-LAST:event_NamejFormattedTextFieldKeyReleased

    private void NamejFormattedTextFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_NamejFormattedTextFieldFocusLost
        Object Id=buscon.SearchIdByName(NamejFormattedTextField.getText());        
        if(!Id.equals(-1)){
           IdjFormattedTextField.setText(""+Id);
        }else{
            JOptionPane.showMessageDialog(null, "Ops.. no se encuentra producto", "advertencia", JOptionPane.OK_OPTION);
        }
    }//GEN-LAST:event_NamejFormattedTextFieldFocusLost

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton AdvanceEdicionjButton;
    private javax.swing.JButton DeletejButton;
    private javax.swing.JTable EditjTable;
    private javax.swing.JFormattedTextField IdjFormattedTextField;
    private javax.swing.JFormattedTextField NamejFormattedTextField;
    private javax.swing.JFormattedTextField NewNamejFormattedTextField;
    private javax.swing.JFormattedTextField NewPricejFormattedTextField;
    private javax.swing.JButton OkjButton;
    private javax.swing.JButton SearchjButton;
    private javax.swing.JComboBox TipojComboBox;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables

    private void integrity() {
//        new RegexFormatter(RegularExpression.WORDS).install(NamejFormattedTextField);
        new RegexFormatter(RegularExpression.NATURAL_NUMBERS).install(IdjFormattedTextField);
        new RegexFormatter(RegularExpression.NATURAL_NUMBERS).install(NewPricejFormattedTextField);
        new RegexFormatter(RegularExpression.WORDS).install(NewNamejFormattedTextField);
    }
}
